//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Files.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;
using System.Web.UI;

namespace CommunityServer.ControlPanel.FileAdmin
{
	/// <summary>
	///		Summary description for FolderEditControl.
	/// </summary>
	public class FolderEditControlDetails : BaseFilesAdminControl
	{

		#region Public Properties

		private int sectionID = -1;
		public int SectionID
		{
			get { return sectionID; }
			set { sectionID = value;}
		}

		#endregion

		#region Child Controls

		protected CommunityServer.Controls.Style ControlPanelStyle;
		protected YesNoRadioButtonList ynIsActive;
		protected YesNoRadioButtonList ynIsSearchable;
		protected TextBox AppKey, Name;
		protected TextBox Owners;
		protected HelpIcon HelpIcon1;
		protected FormLabel Formlabel6;
		protected HelpIcon Helpicon2;
		protected FormLabel Formlabel1;
		protected HelpIcon Helpicon3;
		protected FormLabel Formlabel16;
		protected HelpIcon Helpicon4;
		protected FormLabel Formlabel17;
		protected RequiredFieldValidator AppKeyValidator;
		protected HelpIcon Helpicon5;
		protected FormLabel Formlabel18;
		protected HelpIcon Helpicon6;
		protected FormLabel Formlabel2;
		protected DropDownList AdminGroupList;
		protected RequiredFieldValidator AdminGroupValidator;
		protected HelpIcon Helpicon7;
		protected FormLabel Formlabel3;
		protected ResourceControl Resourcecontrol38;
		protected FilterLanguageDropDownList DefaultLanguage;
		protected Literal AppKeyUrlPrefix;
		protected Literal AppKeyUrlSuffix;
		
		CSContext context = CSContext.Current;
		
		#endregion

		private bool IsNew
		{
			get{return sectionID <= 0;}
		}

		private void Page_Load(object sender, EventArgs e)
		{
			sectionID = context.GetIntFromQueryString("SectionID", -1);

			string appKeyUrl = Globals.GetSiteUrls().UrlData.FormatUrl("files_ViewFolder", "???");
			if (appKeyUrl.ToLower().EndsWith("/default.aspx"))
				appKeyUrl = appKeyUrl.Substring(0, appKeyUrl.Length - 13);

			int index = appKeyUrl.IndexOf("???");
			if (index > -1)
			{
				AppKeyUrlPrefix.Text = appKeyUrl.Substring(0, index);
				AppKeyUrlSuffix.Text = appKeyUrl.Substring(index + 3);
			}


			if(!Page.IsPostBack)
			{
				BindGroupList();
				BindSettingsData();
			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		private void BindGroupList()
		{
			ArrayList groups = CommunityServer.Files.Components.Folders.GetFolderGroups(true, true, false); 
			foreach(Group group in groups)
				AdminGroupList.Items.Add(new ListItem(group.Name, group.GroupID.ToString()));
		}

		#region Folder Settings

		void BindSettingsData()
		{
			Folder f;

			if(IsNew)
			{
				f = new Folder();
			}
			else
			{
				f = CommunityServer.Files.Components.Folders.GetFolder(this.sectionID, false);
				AdminGroupList.Items.FindByValue(f.GroupID.ToString()).Selected = true;
			}

			
			ynIsActive.SelectedValue = f.IsActive;
			ynIsSearchable.SelectedValue = f.IsSearchable;
			AppKey.Text = f.ApplicationKey;
			Owners.Text = f.Owners;
			Name.Text = f.Name;

			DefaultLanguage.SelectedValue = f.DefaultLanguage;
		}

		private Folder Update() 
		{
			Folder f = CommunityServer.Files.Components.Folders.GetFolder(this.sectionID, false);

			if(f.ApplicationKey != AppKey.Text.ToLower())
			{
				string formattedKey = null;
				Globals.ValidateApplicationKey(AppKey.Text, out formattedKey);
                
				if(f.ApplicationKey != formattedKey)
				{
					if(CheckForDuplicate(formattedKey))
					{
						AppKeyValidator.IsValid = false;
						return null;
					}

					f.ApplicationKey = formattedKey;
				}
			}

			f.IsActive =  ynIsActive.SelectedValue;
			f.Owners = Owners.Text;
			f.IsSearchable =  ynIsSearchable.SelectedValue;
			f.Name = Name.Text;
			f.GroupID = int.Parse(AdminGroupList.SelectedValue) ;
			f.Owners =  Owners.Text;
			f.DefaultLanguage = DefaultLanguage.SelectedValue;

			CommunityServer.Files.Components.Folders.Update(f);

			return f;
		}

		private Folder Create()
		{
			//Validate the application key
			string formattedKey = null;
			if(Globals.IsNullorEmpty(AppKey.Text))
				AppKey.Text = Name.Text;

			Globals.ValidateApplicationKey(AppKey.Text, out formattedKey);

			if(CheckForDuplicate(formattedKey))
			{
				AppKeyValidator.IsValid = false;
				return null;
			}
			Folder f = new Folder();
			f.ApplicationKey = formattedKey;
	
			//Set remaining gallery settings
			f.GroupID = int.Parse(AdminGroupList.SelectedValue) ;
			f.Name = Name.Text;
			f.ApplicationKey = formattedKey;
			f.IsActive = ynIsActive.SelectedValue;
			f.SettingsID = context.SiteSettings.SettingsID;
			f.ApplicationType = ApplicationType.FileGallery;
			f.DefaultLanguage = DefaultLanguage.SelectedValue;

			if(!Globals.IsNullorEmpty(Owners.Text))
				f.Owners = Owners.Text;
			
			f.IsSearchable =  ynIsSearchable.SelectedValue;

			//Create The Blog
			f.SectionID = CommunityServer.Files.Components.Folders.Create(f);

			//Update the page SectionID for saving the permissions
			this.SectionID = f.SectionID;

			return f;
		}

		protected bool CheckForDuplicate(string applicationKey)
		{
			// Check to make sure sure it isn't a duplicate blog
			foreach(Folder f in CommunityServer.Files.Components.Folders.GetFolders(false, true))
				if(string.Compare(f.ApplicationKey, applicationKey, true) == 0)
					return true;

			return false;
		}

		#endregion

		public Folder Save()
		{
			//Validate we are an Admin
			if(!context.User.IsFileAdministrator)
				throw new CSException(CSExceptionType.AdministrationAccessDenied);

			if(IsNew)
				return Create();
			else
				return Update();

		}


	}
}
